<template >
  <div class="notice-box">
    <div class="notice-box-filter">
      <el-form ref="form" label-width="80px">
        <div class="table-filter table-filter-home">
          <div>
            <el-form-item label="发布时间">
              <el-date-picker
                v-model="time"
                clearable
                type="datetimerange"
                value-format="yyyyMMddHHmm"
                format="yyyy-MM-dd HH:mm"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                popper-class="table-date-timerange"
                :default-time="['00:00:00']"
              />
            </el-form-item>
          </div>
          <div>
            <el-form-item label="签收状态">
              <dg-select v-model="condition.qszt.value" :data="constant.BM_QSZT" clearable />
            </el-form-item>
          </div>
          <div>
            <el-form-item label="公告类型">
              <dg-select v-model="condition.gglx.value" :data="constant.DM_GGXX_GGLX" clearable />
            </el-form-item>
          </div>
          <div>
            <el-form-item label="关键词">
              <el-input v-model="condition.keyword.value" placeholder="标题、正文、创建人" clearable />
            </el-form-item>
          </div>
          <div></div>
          <div>
            <el-form-item class="operate-btns">
              <el-button type="primary" size="mini" @click="() => $refs.table.searchForm()">查询</el-button>
              <el-button size="mini" @click="reset">重置</el-button>
            </el-form-item>
          </div>
        </div>
      </el-form>
    </div>
    <div class="notice-box-content">
      <comm-table
        ref="table"
        class="table"
        row-key="qsxxid"
        :condition="condition"
        :columns="columns"
        url="/v1/ggxx/search"
      >
        <template slot-scope="{ row }">
          <el-button type="text" :disabled="row.qszt === '1'" @click="onSign(row)">签收</el-button>
          <el-divider direction="vertical" v-permission="['XXXX_PRINT']"></el-divider>
          <el-button type="text" v-permission="['XXXX_PRINT']" @click="openPrintPage(row)">打印</el-button>
        </template>
      </comm-table>
    </div>
  </div>
</template>
<script>
import { signIn } from '@/api/notice';
import { deepClone } from '@/utils/common';
import { mapState } from 'vuex';
import { BM_QSZT, DM_GGXX_GGLX, BM_DEPT } from '@/common/codeDic';
import {printYaxx} from "@/api/common";
export default {
  name: 'NoticeInfomation',
  data() {
    return {
      // 列表查询条件
      condition: {
        fbsj: {
          value: '',
          op: '='
        },
        qszt: {
          value: '',
          op: '='
        },
        gglx: {
          value: '',
          op: '='
        },
        keyword: {
          value: '',
          op: 'like'
        }
      },
      // 表格项
      columns: [
        { type: 'index', label: '序号', width: 70 },
        { label: '发布时间', prop: 'fbsj' },
        { label: '公告类型', prop: 'gglx', code: DM_GGXX_GGLX },
        {
          label: '标题',
          render: (h, row) => {
            const _this = this;
            return h(
              'span',
              {
                class: 'v-link',
                on: {
                  click() {
                    // 隐藏tooltip(由于不自动消失)
                    $('.el-tooltip__popper').css('display', 'none');
                    _this.$router.push({ name: 'NoticeDetail', params: { id: row.id } });
                  }
                }
              },
              row.ggbt
            );
          },
          width: 300
        },
        { label: '签收状态', prop: 'qszt', code: BM_QSZT },
        { label: '发布单位', prop: 'createDept', code: BM_DEPT },
        { label: '发布人', prop: 'createUsername' },
        { label: '操作', type: 'operate' }
      ],
      time: null
    };
  },

  watch: {
    time(val) {
      this.condition.fbsj.value = val ? `${val[0]},${val[1]}` : '';
    }
  },
  computed: {
    ...mapState(['constant'])
  },
  activated() {
    this.$refs.table.searchForm();
  },
  created() {
    this.$store.dispatch('getConstant', ['DM_GGXX_GGLX', 'BM_QSZT']);
    this.$store.dispatch('getConstantTree', ['BM_DEPT']);
    this.cloneCondition = deepClone(this.condition);
  },
  methods: {
    onSign(row) {
      this.$confirm('确认签收?').then(() => {
        const loadingInstance = this.$loading({
          lock: true,
          text: '签收中...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        signIn(row.qsxxid)
          .then(() => {
            this.$refs.table.searchForm();
            this.$message.success('签收成功');
            loadingInstance.close();
          })
          .catch(() => {
            this.$message.warning('签收失败');
            loadingInstance.close();
          });
      });
    },
    /**
     * 重置
     */
    reset() {
      this.time = null;
      this.condition = deepClone(this.cloneCondition);
      this.$nextTick(() => {
        this.$refs.table.searchForm();
      });
    },
    /**
     * 打印
     */
    openPrintPage(row){
      let doc_name = '每日要情';
      let doc_id = row.id;
      let type = '2';
      printYaxx(doc_id, doc_name, type);
    }
  }
};
</script>
<style scoped lang='scss'>
.notice-box-content {
  background: #fff;
  margin-top: 0.6rem;
  .table {
    padding: 20px;
  }
}
</style>
